﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.Linq;
using System.IO;
using QuickTasks.ViewModels.Properties;
using LocalSettings;
using QuickTasks.ViewModels.Tables;
using System.Collections.ObjectModel;
namespace QuickTasks.ViewModels.Data
{

    public class ToDoListData
    {
     

        #region events

        public void saveToDoList(string name, string description, string catagiry, string datemonth,string time,string list)
        {
            
            if (list != "")
            {
                try
                {
                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                       IQueryable<Todo> listid = from a in context.Todo where a.id.ToString() == list select a;
                       
                            Todo todo = listid.FirstOrDefault();
                            {
                               
                  
                                 todo.name = name;
                                 todo.description = description;
                                 todo.category = catagiry;
                                 todo.place = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                                 todo.datemonth = Convert.ToDateTime(datemonth).ToShortDateString();
                                 todo.time = Convert.ToDateTime(time).ToShortTimeString();

                            };
                           
                            context.SubmitChanges();
                        }
                    }
                    
                
                catch(Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in saveToDoList method In ToDoListData file", ex);
                }
            }
            else
            {
                try
                {
                    int tid=0;
                    string DefaultPlace = "";
                    if (IsolatedSettings.getSettingsStringValue("DefaultPlace") != "")
                    {
                        DefaultPlace = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                    }
                    else
                    {
                        DefaultPlace = "1";
                    }

                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        var xquery = from a in context.Todo
                                     orderby a.id descending
                                     select a.id;
                        if (xquery.Count() <= 0)
                            {
                                tid = 1;
                            }
                        else
                        {
                            var xquery1 = from a in context.Todo
                                         orderby a.id descending
                                         select a;
                           
                           
                                foreach (var query in xquery1)
                                {
                                   
                                    tid = Convert.ToInt32(query.id) + 1;
                                    break;
                                }
                        }
                        Todo todo = new Todo()
                        {
                            id = tid,
                            name = name,
                            description = description,
                            category = catagiry,
                            place = DefaultPlace,
                            datemonth = datemonth,
                            time=time

                        };
                        context.Todo.InsertOnSubmit(todo);
                        context.SubmitChanges();

                    }

                }

                catch (Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in saveToDoList method In ToDoListData file", ex);
                }
            }
        }

        public string[]  savereminder(string name, string description, string catagiry, string datemonth, string time, string list)
        {
            string[] type = new string[4];
            if (list != "")
            {
                try
                {
                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        IQueryable<Reminders> listid = from a in context.reminderalr where a.id.ToString() == list select a;

                        Reminders reminder = listid.FirstOrDefault();
                        {


                            reminder.title = name;
                            reminder.description = description;
                            //reminder.category = catagiry;
                            reminder.place = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                            reminder.begintime = Convert.ToDateTime(datemonth).Add(TimeSpan.Parse(time)).ToString();
                            reminder.endtime = Convert.ToDateTime(datemonth).Add(TimeSpan.Parse(time)).AddMinutes(1).ToString();
                            type[0] = reminder.type;
                            type[1] = reminder.Rguid;
                            type[2] = reminder.begintime;
                            type[3] = reminder.id.ToString();
                        };

                        context.SubmitChanges();
                    }
                    //return type;
                }


                catch (Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in savereminder method In ToDoListData file", ex);
                }
               
            }
            return type;
          
        }
        public List<ToDoListProper> LoadTodolistName()
        {

            List<ToDoListProper> objToDoListProper = new List<ToDoListProper>();

            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        var querytodolist = from i in context.Todo select i;
                        foreach (var objlists in querytodolist)
                        {
                            ToDoListProper objToDolist = new ToDoListProper();
                            objToDolist.Id = objlists.id.ToString();
                            objToDolist.Name = objlists.name;
                            DateTime dt = Convert.ToDateTime(objlists.datemonth);
                            objToDolist.Date = dt.Day + "/" + dt.Month;
                            objToDoListProper.Add(objToDolist);
                        }
                }
                
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadTodolistName method In ToDoListData file", ex);
            }
               
            return objToDoListProper;
        }

        public ObservableCollection<ToDoListProper> LoadToDoListCity(string id)
        {
            ObservableCollection<ToDoListProper> objToDoListProper = new ObservableCollection<ToDoListProper>();
            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
            {
                var querytodolist = from i in context.Todo where i.place == id orderby i.time,i.datemonth descending select i;
                foreach (var objlists in querytodolist)
                {
                    ToDoListProper objToDolist = new ToDoListProper();
                    objToDolist.Id = objlists.id.ToString();
                    objToDolist.Name = objlists.name;
                   
                    objToDoListProper.Add(objToDolist);
                }
            }
            
            return objToDoListProper;
        }
        public List<ToDoListProper> LoadCompletedTasks(int taskcount)
        {
            List<ToDoListProper> objToDoListProper = new List<ToDoListProper>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {

                    var completedquery = (from i in context.CTodo select i).Take(taskcount);
                    foreach (var list in completedquery)
                    {
                        ToDoListProper objToDoList = new ToDoListProper();
                        objToDoList.Id = (list.id).ToString();
                        objToDoList.Name = list.name;
                        objToDoList.Date = System.DateTime.Now.ToShortDateString();

                        objToDoListProper.Add(objToDoList);

                    }
                    int totaltasks = (from i in context.CTodo select i).Count();
                    if (objToDoListProper.Count == taskcount)
                    {
                        ToDoListProper wantmore = new ToDoListProper();
                        wantmore.Name = "get more";
                        objToDoListProper.Add(wantmore);
                    }


                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadCompletedTasks method In ToDoListData file", ex);
            }
            return objToDoListProper;
        }

        #endregion
    }
}
